home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 114_01 / ed4.bds < prev    next >
Text File  |  1987-07-13  |  13KB  |  710 lines

  1. /*
  2.  * Screen editor:  window module
  3.  *
  4.  * Source:  ed4.cc
  5.  * Version: August 21, 1981.
  6.  */
  7.  
  8. /* define global constants, variables */
  9.  
  10. #include ed.h
  11. #include bdscio.h
  12. #include ed1.ccc
  13. #include edext.cc
  14.  
  15. /* data global to this module -----
  16.  
  17. char    editbuf[MAXLEN];    the edit buffer
  18. int    editp;            cursor: buffer index
  19. int    editpmax;        length of buffer
  20. int    edcflag;        buffer change flag
  21.  
  22. ----- */
  23.  
  24. /* abort any changes made to current line */
  25.  
  26. edabt()
  27. {
  28.     /* get unchanged line and reset cursor */
  29.     edgetln();
  30.     edredraw();
  31.     edbegin();
  32.     edcflag = NO;
  33. }
  34.  
  35. /* put cursor at beginning of current line */
  36.  
  37. edbegin()
  38. {
  39.     editp = 0;
  40.     outxy(0,outgety());
  41. }
  42.  
  43. /* change editbuf[editp] to c
  44.  * don't make change if line would become to long
  45.  */
  46.  
  47. edchng(c) char c;
  48. {
  49. char oldc;
  50. int k;
  51.     /* if at right margin then insert char */
  52.     if (editp >= editpmax) {
  53.         edins(c);
  54.         return;
  55.     }
  56.     /* change char and print length of line */
  57.     oldc = editbuf[editp];
  58.     editbuf[editp] = c;
  59.     fmtadj(editbuf,editp,editpmax);
  60.     k = fmtlen(editbuf,editpmax);
  61.     if (k > SCRNW1) {
  62.         /* line would become too long */
  63.         /* undo the change */
  64.         editbuf[editp] = oldc;
  65.         fmtadj(editbuf,editp,editpmax);
  66.     }
  67.     else {
  68.         /* set change flag, redraw line */
  69.         edcflag = YES;
  70.         editp++;
  71.         edredraw();
  72.     }
  73. }
  74.  
  75. /* delete the char to left of cursor if it exists */
  76.  
  77. eddel()
  78. {
  79. int k;
  80.     /* just move left one column if past end of line */
  81.     if (edxpos() < outgetx()) {
  82.         outxy(outgetx()-1, outgety());
  83.         return;
  84.     }
  85.     /* do nothing if cursor is at left margin */
  86.     if (editp == 0) {
  87.         return;
  88.     }
  89.     edcflag = YES;
  90.     /* compress buffer (delete char) */
  91.     k = editp;
  92.     while (k < editpmax) {
  93.         editbuf[k-1] = editbuf[k];
  94.         k++;
  95.     }
  96.     /* update pointers, redraw line */
  97.     editp--;
  98.     editpmax--;
  99.     edredraw();
  100. }
  101.  
  102. /* edit the next line.  do not go to end of buffer */
  103.  
  104. eddn()
  105. {
  106. int oldx;
  107.     /* save visual position of cursor */
  108.     oldx = outgetx();
  109.     /* replace current edit line */
  110.     if (edrepl() != OK) {
  111.         return(ERR);
  112.     }
  113.     /* do not go past last non-null line */
  114.     if (bufnrbot()) {
  115.         return(OK);
  116.     }
  117.     /* move down one line in buffer */
  118.     if (bufdn() != OK) {
  119.         return(ERR);
  120.     }
  121.     edgetln();
  122.     /* put cursor as close as possible on this
  123.      * new line to where it was on the old line.
  124.      */
  125.     editp = edscan(oldx);
  126.     /* update screen */
  127.     if (edatbot()) {
  128.         edsup(bufln()-SCRNL2);
  129.         outxy(oldx, SCRNL1);
  130.     }
  131.     else {
  132.         outxy(oldx, outgety()+1);
  133.     }
  134.     return(OK);
  135. }
  136.  
  137. /* put cursor at the end of the current line */
  138.  
  139. edend()
  140. {
  141.     editp = editpmax;
  142.     outxy(edxpos(),outgety());
  143.     
  144.     /* comment out ----- put cursor at end of screen
  145.     outxy(SCRNW1, outgety());
  146.     ----- end comment out */
  147. }
  148.  
  149. /* start editing line n
  150.  * redraw the screen with cursor at position p
  151.  */
  152.  
  153. edgo(n, p) int n, p;
  154. {
  155.     /* replace current line */
  156.     if (edrepl() == ERR) {
  157.         return(ERR);
  158.     }
  159.     /* go to new line */
  160.     if (bufgo(n) == ERR) {
  161.         return(ERR);
  162.     }
  163.     /* prevent going past end of buffer */
  164.     if (bufatbot()) {
  165.         if (bufup() == ERR) {
  166.             return(ERR);
  167.         }
  168.     }
  169.     /* redraw the screen */
  170.     bufout(bufln(),1,SCRNL1);
  171.     edgetln();
  172.     editp = min(p, editpmax);
  173.     outxy(edxpos(), 1);
  174.     return(OK);
  175. }
  176.  
  177. /* insert c into the buffer if possible */
  178.  
  179. edins(c) char c;
  180. {
  181. int k;
  182.  
  183.     /* do nothing if edit buffer is full */
  184.     if (editpmax >= MAXLEN) {
  185.         return;
  186.     }
  187.     /* fill out line if we are past its end */
  188.     if (editp == editpmax && edxpos() < outgetx()) {
  189.         k = outgetx() - edxpos();
  190.         editpmax = editpmax + k;
  191.         while (k-- > 0) {
  192.             editbuf [editp++] = ' ';
  193.         }
  194.         editp = editpmax;
  195.     }
  196.     /* make room for inserted character */
  197.     k = editpmax;
  198.     while (k > editp) {
  199.         editbuf[k] = editbuf[k-1];
  200.         k--;
  201.     }
  202.     /* insert character. update pointers */
  203.     editbuf[editp] = c;
  204.     editp++;
  205.     editpmax++;
  206.     /* recalculate print length of line  */
  207.     fmtadj(editbuf,editp-1,editpmax);
  208.     k = fmtlen(editbuf,editpmax);
  209.     if (k > SCRNW1) {
  210.         /* line would become too long */
  211.         /* delete what we just inserted */
  212.         eddel();
  213.     }
  214.     else {
  215.         /* set change flag, redraw line */
  216.         edcflag = YES;
  217.         edredraw();
  218.     }
  219. }
  220.  
  221. /* join (concatenate) the current line with the one above it */
  222.  
  223. edjoin()
  224. {
  225. int k;
  226.  
  227.     /* do nothing if at top of file */
  228.     if (bufattop()) {
  229.         return;
  230.     }
  231.     /* replace lower line temporarily */
  232.     if (edrepl() != OK) {
  233.         return;
  234.     }
  235.     /* get upper line into buffer */
  236.     if (bufup() != OK) {
  237.         return;
  238.     }
  239.     k = bufgetln(editbuf, MAXLEN);
  240.     /* append lower line to buffer */
  241.     if (bufdn() != OK) {
  242.         return;
  243.     }
  244.     k = k + bufgetln(editbuf+k, MAXLEN-k);
  245.     /* abort if the screen isn't wide enough */
  246.     if (k > SCRNW1) {
  247.         /* bug fix */
  248.         bufgetln(editbuf,MAXLEN);
  249.         return;
  250.     }
  251.     /* replace upper line */
  252.     if (bufup() != OK) {
  253.         return;
  254.     }
  255.     editpmax = k;
  256.     edcflag = YES;
  257.     if (edrepl() != OK) {
  258.         return;
  259.     }
  260.     /* delete the lower line */
  261.     if (bufdn() != OK) {
  262.         return;
  263.     }
  264.     if (bufdel() != OK) {
  265.         return;
  266.     }
  267.     if (bufup() != OK) {
  268.         return;
  269.     }
  270.     /* update the screen */
  271.     if (edattop()) {
  272.          edredraw();
  273.     }
  274.     else {
  275.         k = outgety() - 1;
  276.         bufout(bufln(),k,SCRNL-k);
  277.         outxy(0,k);
  278.         edredraw();
  279.     }
  280. }
  281.  
  282. /* delete chars until end of line or c found */
  283.  
  284. edkill(c) char c;
  285. {
  286. int k,p;
  287.     /* do nothing if at right margin */
  288.     if (editp == editpmax) {
  289.         return;
  290.     }
  291.     edcflag = YES;
  292.     /* count number of deleted chars */
  293.     k = 1;
  294.     while ((editp+k) < editpmax) {
  295.         if (editbuf[editp+k] == c) {
  296.             break;
  297.         }
  298.         else {
  299.             k++;
  300.         }
  301.     }
  302.     /* compress buffer (delete chars) */
  303.     p = editp+k;
  304.     while (p < editpmax) {
  305.         editbuf[p-k] = editbuf[p];
  306.         p++;
  307.     }
  308.     /* update buffer size, redraw line */
  309.     editpmax = editpmax-k;
  310.     edredraw();
  311. }
  312.  
  313. /* move cursor left one column.
  314.  * never move the cursor off the current line.
  315.  */
  316.  
  317. edleft()
  318. {
  319. int k;
  320.  
  321.     /* if past right margin, move left one column */
  322.     if (edxpos() < outgetx()) {
  323.         outxy(max(0, outgetx()-1), outgety());
  324.     }
  325.     /* inside the line.  move left one character */
  326.     else if (editp != 0) {
  327.         editp--;
  328.         outxy(edxpos(),outgety());
  329.     }
  330. }
  331.  
  332. /* insert a new blank line below the current line */
  333.  
  334. ednewdn()
  335. {
  336. int k;
  337.     /* make sure there is a current line and 
  338.      * put the current line back into the buffer.
  339.      */
  340.     if (bufatbot()) {
  341.         if (bufins(editbuf,editpmax) != OK) {
  342.             return;
  343.         }
  344.     }
  345.     else if (edrepl() != OK) {
  346.             return;
  347.     }
  348.     /* move past current line */
  349.     if (bufdn() != OK) {
  350.         return;
  351.     }
  352.     /* insert place holder:  zero length line */
  353.     if (bufins(editbuf,0) != OK) {
  354.         return;
  355.     }
  356.     /* start editing the zero length line */
  357.     edgetln();
  358.     /* update the screen */
  359.     if (edatbot()) {
  360.         /* note: bufln()  >= SCRNL */
  361.         edsup(bufln()-SCRNL2);
  362.         outxy(edxpos(),SCRNL1);
  363.     }
  364.     else {
  365.         k = outgety();
  366.         bufout(bufln(),k+1,SCRNL1-k);
  367.         outxy(edxpos(),k+1);
  368.     }
  369. }
  370.  
  371. /* insert a new blank line above the current line */
  372.  
  373. ednewup()
  374. {
  375. int k;
  376.     /* put current line back in buffer */
  377.     if (edrepl() != OK) {
  378.         return;
  379.     }
  380.     /* insert zero length line at current line */
  381.     if (bufins(editbuf,0) != OK) {
  382.         return;
  383.     }
  384.     /* start editing the zero length line */
  385.     edgetln();
  386.     /* update the screen */
  387.     if (edattop()) {
  388.         edsdn(bufln());
  389.         outxy(edxpos(),1);
  390.     }
  391.     else {
  392.         k = outgety();
  393.         bufout(bufln(),k,SCRNL-k);
  394.         outxy(edxpos(),k);
  395.     }
  396. }
  397.  
  398. /* move cursor right one character.
  399.  * never move the cursor off the current line.
  400.  */
  401.  
  402. edright()
  403. {
  404.     /* if we are outside the line move right one column */
  405.     if (edxpos() < outgetx()) {
  406.         outxy (min(SCRNW1, outgetx()+1), outgety());
  407.     }
  408.     /* if we are inside a tab move to the end of it */
  409.     else if (edxpos() > outgetx()) {
  410.         outxy (edxpos(), outgety());
  411.     }
  412.     /* move right one character if inside line */
  413.     else if (editp < editpmax) {
  414.         editp++;
  415.         outxy(edxpos(),outgety());
  416.     }
  417.     /* else move past end of line */
  418.     else {
  419.         outxy (min(SCRNW1, outgetx()+1), outgety());
  420.     }
  421. }
  422.  
  423. /* split the current line into two parts.
  424.  * scroll the first half of the old line up.
  425.  */
  426.  
  427. edsplit()
  428. {
  429. int p, q;
  430. int k;
  431.  
  432.     /* indicate that edit buffer has been saved */
  433.     edcflag = NO;
  434.     /* replace current line by the first half of line */
  435.     if (bufatbot()) {
  436.         if (bufins(editbuf, edi